package com.caiyigo.baihua.fetion;

import java.util.Map;

import javax.swing.SwingUtilities;

import net.solosky.maplefetion.event.ActionEvent;
import net.solosky.maplefetion.event.action.FailureEvent;
import net.solosky.maplefetion.event.action.success.SendChatMessageSuccessEvent;

import com.caiyigo.baihua.entity.Xuesheng;

public class Result {
	private String taskName;
	private String mobile;
	private String result;
	private String zkz;

	public String getZkz() {
		return zkz;
	}

	public void setZkz(String zkz) {
		this.zkz = zkz;
	}

	private ActionEvent event;
	private Integer order;
	private Map<String, Xuesheng> xueshengMapKeyAtMobile;
	private String cause;

	public String getCause() {
		return cause;
	}

	public void setCause(String cause) {
		this.cause = cause;
	}

	public Integer getOrder() {
		return order;
	}

	public void setOrder(Integer order) {
		this.order = order;
	}

	public ActionEvent getEvent() {
		return event;
	}

	public void setEvent(ActionEvent event) {
		this.event = event;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public String getMobile() {
		return mobile;
	}

	public Result(String mobile, ActionEvent event,
			Map<String, Xuesheng> xueshengMapKeyAtMobile) {
		super();
		this.mobile = mobile;
		this.event = event;
		this.result = test();
		this.xueshengMapKeyAtMobile = xueshengMapKeyAtMobile;
		appendNewText(StringUtils.formatYuanyin(getResult(),
				buildAppendMessage(mobile)));
		setZkz(xueshengMapKeyAtMobile.get(mobile).getXsZkz());
	}

	public Result() {
		// TODO 自动生成的构造函数存根
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	private String test() {
		if (null == this.event) {
			setOrder(2);
			return "-14";
		}
		switch (event.getEventType()) {
		case SUCCESS:
			SendChatMessageSuccessEvent evt = (SendChatMessageSuccessEvent) event;
			if (evt.isSendToMobile()) {
				setOrder(1);
				return ("1");

			} else if (evt.isSendToClient()) {
				setOrder(1);
				return ("2");

			}
			break;

		case FAILURE:
			FailureEvent evt2 = (FailureEvent) event;
			switch (evt2.getFailureType()) {
			case BUDDY_NOT_FOUND:
				setOrder(2);
				return ("-1");
			case USER_NOT_FOUND:
				setOrder(2);
				return ("-2");
			case SIPC_FAIL:
				setOrder(2);
				return ("-3");
			case UNKNOWN_FAIL:
				setOrder(2);
				return ("-4");

			case SERVER_BUSY:
				setOrder(2);
				return ("-8");

			case SERVER_ERROR:
				setOrder(2);
				return ("-9");

			case NOT_ACCEPTABLE:
				setOrder(2);
				return ("-10");

			case REQEUST_FAIL:
				setOrder(2);
				return ("-11");

			case INVALID_ACCOUNT:
				setOrder(2);
				return ("-12");

			case BUDDY_RELATION_FORBIDDEN:
				setOrder(2);
				return ("-13");

			default:
				setOrder(2);
				return ("-14");
			}

		case SYSTEM_ERROR:
			setOrder(2);
			return ("-5");

		case TIMEOUT:
			setOrder(2);
			return ("-6");

		case TRANSFER_ERROR:
			setOrder(2);
			return ("-7");
		}
		setOrder(2);
		return "-14";
	}

	public void appendNewText(final String txt) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				fetion.helpMessage.append("\n" + txt);
				fetion.taInfo.setText(fetion.helpMessage.toString());
				fetion.taInfo.setCaretPosition(fetion.taInfo.getDocument()
						.getLength());

			}
		});
	}

	public String buildAppendMessage(String mobile) {
		String name = xueshengMapKeyAtMobile.get(mobile).getXsXm();
		return StringUtils.getFullName(name, true) + "--" + mobile;

	}
}
